package com.dzl.admin.service.impl;

import com.dzl.admin.dao.SysRolePermissionMapper;
import com.dzl.admin.pojo.SysRolePermission;
import com.dzl.admin.service.SysRolePermissionService;
import com.dzl.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 角色权限表(SysRolePermission)表服务实现类
 *
 * @author caobucao
 * @since 2021-05-13 15:49:59
 */
@Service("sysRolePermissionService")
public class SysRolePermissionServiceImpl implements SysRolePermissionService {

    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public Integer insertSysRolePermissionList(Integer roleId, String[] permissionIds) {
        return sysRolePermissionMapper.insertSysRolePermissionList(roleId, permissionIds);
    }

    @Override
    public Integer deleteRolePermissionByRoleId(Integer id) {
        return sysRolePermissionMapper.deleteRolePermissionByRoleId(id);
    }

    @Override
    public Integer saveBatchRolePermission(List<Integer> permissionIds, Integer roleId) {
        List<SysRolePermission> rolePermissions=new ArrayList<>();
        Integer count=0;
        if(EmptyUtils.isNotEmpty(permissionIds) && roleId!=null){
            for(Integer permissionId:permissionIds){
                SysRolePermission rolePermission=new SysRolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                rolePermission.setCreateTime(new Date());
                rolePermission.setUpdateTime(new Date());
                rolePermissions.add(rolePermission);
            }
            count=sysRolePermissionMapper.insertList(rolePermissions);
        }
        return count;
    }

    @Override
    public List<Integer> queryRolePermissionByRoleId(Integer roleId) {
        return sysRolePermissionMapper.queryRolePermissionByRoleId(roleId);
    }
}
